import Koa from 'koa2'
import KoaStatic from 'koa-static'
import bodyParser from 'koa-bodyparser'
import Router from 'koa-router'
import cors from '@koa/cors'
import globalConfig from './config'
import route from './routes'

const start = async () => {
  const app = new Koa()
  const host = process.env.HOST || globalConfig.app.host
  const port = process.env.PORT || globalConfig.app.port
  const router = new Router()

  app.use(cors())
  app.use(bodyParser({
    jsonLimit: '5mb', // 控制body的parse转换大小 default 1mb
    formLimit: '4096kb' //  控制你post的大小  default 56kb
  }))
  app.use(KoaStatic('.'))
  router.use('', route.routes())
  app
    .use(router.routes())
    .use(router.allowedMethods())

  app.listen(port, host)
  console.log('Server listening on ' + host + ':' + port) // eslint-disable-line no-console
}

start()